﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eTruck.Helpers;

namespace eTruck
{
    public partial class Index : System.Web.UI.Page
    {
        public DataSet ds;
        public string[] ParentMenus;
        eTruck.BLL.Modules bll = new eTruck.BLL.Modules();

        string login_url = "/Views/Users/Login.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            //检查是否已经有cookies
            //不存在直接跳入登录界面
           
                if (Session["UserName"] == null)
                {
                    Session["UserName"] = "";
                }
                LoginName.Text = Session["UserName"].ToString();
            if (CookiesHelper.GetCookie(CommonHelper.LoginCookieName) == null)
            {
                MessageBoxHelper.MessageBox(Page, "", login_url);
                return;
            }
            try
            {
                ds = bll.GetUserModule(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
                
                string tempstr = "";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (!tempstr.Contains(ds.Tables[0].Rows[i][2].ToString() + "#"))
                    {
                        tempstr = tempstr + ds.Tables[0].Rows[i][2].ToString() + "#";
                    }
                }
                tempstr = tempstr.Substring(0, tempstr.Length - 1);
                ParentMenus = tempstr.Split('#');
            }
            catch
            {
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            BLL.Users users = new BLL.Users();
            string oldpwd = txtOldPwd.Text.Trim();
            string newpwd = txtNewPwd.Text.Trim();
            int userid = Convert.ToInt32(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
            string result = "";
            result = users.UpdatePassword(userid, oldpwd, newpwd);
            if (result.Length > 0)
            {
                MessageBoxHelper.MessageBox(Page, "修改失败，请重试:" + result, "#");
            }
            else
            {
                CookiesHelper.ClearCookie(CommonHelper.LoginCookieName);
                MessageBoxHelper.MessageBox(Page, "修改成功，请重新登录", login_url);
            }
        }

        protected void lbnLogout_Click(object sender, EventArgs e)
        {
            CookiesHelper.ClearCookie(CommonHelper.LoginCookieName);
            MessageBoxHelper.MessageBox(Page, "请重新登录", login_url);
        }
    }
}